//
//  NewsViewController.m
//  时光电影项目
//
//  Created by 墨狂之逸才 on 15/7/17.
//  Copyright (c) 2015年 墨狂之逸才. All rights reserved.
//

#import "NewsViewController.h"
#import "NewsModal.h"
#import "loadData.h"
#import "Header.h"
#import "UIViewExt.h"
#import "UIImageView+WebCache.h"
#import "NewsImageListViewController.h"

@interface NewsViewController ()
{
    NSMutableArray *_newsModalArray;
    UIImageView *_headerImageView;
    UILabel *_headerLabel;
}
@end

@implementation NewsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    //设置导航栏为半透明，以便使tableView的内容在延展的情况下，自动调整内容。
    self.navigationController.navigationBar.translucent = YES;
    
//    
//    NSArray *nameArray = [NSArray array];
//    nameArray = @[@"首页",@"新闻",@"Top250",@"影院",@"更多"];
//    
//    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.navigationController.navigationBar.frame.size.width, self.navigationController.navigationBar.frame.size.height)];
//    label.text = @"新闻";
//    label.textColor = [UIColor whiteColor];
//    label.textAlignment = NSTextAlignmentCenter;
    
    self.navigationItem.title = @"新闻";
//    [self.navigationController.navigationBar addSubview:label];
    
//    self.view.backgroundColor = [UIColor grayColor];
    
    
    
   
//    [self _creatScrollView];
    [self _creatNewTableView];
    
    [self _creatHeaderView];
    
    
     [self _loadData];
 
    
}


#pragma mark - downLoad下载数据
- (void)_loadData
{
    
    _newsModalArray = [[NSMutableArray alloc]init];
//    NSString *paths = [[NSBundle mainBundle]pathForResource:@"news_list.json" ofType:nil];
//    
//    
//    //NSData直接存储二进制数据的
//    NSData *data = [NSData dataWithContentsOfFile:paths];
//    
//    //解析json数据,将json数据存入字典或数组中
//    NSArray *newsArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    NSArray *newsArray = [loadData loadDataWithResourceName:@"news_list.json"];
    
    for (NSDictionary *dic in newsArray) {
        
        //把数据传递给模型数据newsModal
        NewsModal *newsModal = [[NewsModal alloc]init];
        
//        newsModal.title = [dic objectForKey:@"title"];
//        newsModal.summary = [dic objectForKey:@"summary"];
//        newsModal.image = [dic objectForKey:@"image"];
        
        
        [newsModal setValuesForKeysWithDictionary:dic];
        
        
        [_newsModalArray addObject:newsModal];
        
        


    }
    
    NewsModal *newsModal = _newsModalArray[0];
    
    _headerLabel.text = newsModal.title;
    
    
    NSURL *url = [NSURL URLWithString:newsModal.image];
    [_headerImageView sd_setImageWithURL:url];
   
    //避免与头条新闻重复，而不再cell里面显示了
    [_newsModalArray removeObjectAtIndex:0];
    
    
}

//因为tableView是scrollView的子类，所以可以监控其滑动的方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    CGFloat offY = scrollView.contentOffset.y;
    NSLog(@"%f",offY);
    
    
    if (offY > -64 ) {
          _headerImageView.top = - offY;
        _headerLabel.bottom = _headerImageView.bottom;
    }
    else
    {
     
        CGFloat newHeight = 200+abs(offY)-64;
        CGFloat newWidth = newHeight/200*kwidth;
        
        _headerImageView.frame = CGRectMake(-(newWidth-kwidth)/2, 64, newWidth, newHeight);
           _headerLabel.bottom = _headerImageView.bottom;
        
    }
    
    
}


#pragma mark -创建headerView，展示图片
- (void)_creatHeaderView
{
    
    //headerVIew作为cell的tableHeaderView，并且设置为透明
    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 200)];
//    headerView.backgroundColor = [UIColor redColor];
    headerView.backgroundColor = [UIColor clearColor];
    
    _newsTableView.tableHeaderView = headerView;
    
    //然后在self.view上添加一个imageVIew的图片，并且设置相关cell滑动时的动作
    
    
    _headerImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 64, kwidth, 200)];
//    _headerImageView.image = [UIImage imageNamed:@"6"];
    
    
    
    _headerLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, kwidth, 30)];
    _headerLabel.bottom = _headerImageView.bottom;
//    _headerLabel.text = @"fsfsd";
    _headerLabel.backgroundColor = [UIColor grayColor];
    
    
    
    //添加到self.view上
    [self.view addSubview:_headerImageView];
    [self.view addSubview:_headerLabel];
}


#pragma mark - 创建图片和文字新闻
- (void)_creatNewTableView
{
    _newsTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    
    _newsTableView.delegate = self;
    _newsTableView.dataSource = self;
    
//    _newsTableView.hidden = YES;
    
    [self.view addSubview:_newsTableView];
    
    //注册nib
    UINib *nib = [UINib nibWithNibName:@"NewsCell" bundle:[NSBundle mainBundle]];
    
    //让tableView来注册nib
    [_newsTableView registerNib:nib forCellReuseIdentifier:@"newsCell"];
    
}

#pragma mark -tableView的相关设置
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _newsModalArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NewsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"newsCell" forIndexPath:indexPath];
    
    //    cell.textLabel.text = @"ss";
    
    //从数据数组中，获取对应一行的的cell要显示的内容
    NewsModal *newsModal = _newsModalArray[indexPath.row];
    
    //具体的cell多内容不在这里设置，而在其子类NewsModal里面做具体的设置，将cell数据源的数据传递到newsModal里面的newsModal属性里面去，
    cell.newsModal = newsModal;
    
//    cell.textLabel.text = @"dd";
//    cell.backgroundColor = [UIColor redColor];
    NSLog(@"cellForRowAtIndexPath");
    
    return cell;
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}


#pragma mark - 返回cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 90;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NewsImageListViewController *vc = [[NewsImageListViewController alloc]init];
    
    [self.navigationController pushViewController:vc animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
